﻿using BW.ME.Exam.Domain.Entities;
using BW.ME.Exam.ErrorCode;
using BW.ME.Exam.Infrastructure.Interfaces;
using BW.ME.Exam.Read.Api.Applications.Commands.FormQueryCommand;
using BW.ME.Exam.Read.Api.Dtos;
using MediatR;

namespace BW.ME.Exam.Read.Api.Applications.CommandHandlers.FormQueryCommandHandler
{
    /// <summary>
    /// 我的表单查询命令处理
    /// </summary>
    public class MyFormQueryCommandHandler : IRequestHandler<MyFormQueryCommand, ApiResult<List<InstanceDTO>>>
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="instanceRepository">实例仓储</param>
        /// <param name="gClassRepository">班级仓储</param>
        public MyFormQueryCommandHandler(IBaseRepository<WF_Instance> instanceRepository, IBaseRepository<GClass> gClassRepository)
        {
            InstanceRepository = instanceRepository;
            GClassRepository = gClassRepository;
        }

        public IBaseRepository<WF_Instance> InstanceRepository { get; }
        public IBaseRepository<GClass> GClassRepository { get; }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<ApiResult<List<InstanceDTO>>> Handle(MyFormQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<InstanceDTO>> result= new ApiResult<List<InstanceDTO>>();
            result.Code = ResultCode.查询成功;
            result.Message=ResultCode.查询成功.ToString();

            var list = from a in InstanceRepository.GetAll()
                       join b in GClassRepository.GetAll() on a.AppId equals b.ClassId
                       select new InstanceDTO
                       {
                           InstanceId=a.InstanceId,
                           AppId=a.AppId,
                           FlowStatus=a.FlowStatus,
                           FlowStatusName=a.FlowStatusName,
                           StaterId=a.StaterId,
                           StarterName=a.StarterName,
                           CurrentNodeNo=a.CurrentNodeNo,
                           CurrentNodeName=a.CurrentNodeName,
                           CurrentOperatorId=a.CurrentOperatorId,
                           CurrentOperatorName=a.CurrentOperatorName,
                           NextNodeNo=a.NextNodeNo,
                           NextNodeName=a.NextNodeName,
                           NextOperatorId=a.NextOperatorId,
                           NextOperatorName=a.NextOperatorName,
                           CreateTime=a.CreateTime,
                           ClassRoom = b.ClassRoom,
                           ClassTeacher=b.ClassTeacher,
                           ZhuTeacher=b.ZhuTeacher,
                       };
           
             if(request.RoleId!=1)
             {
                list = list.Where(x => x.StaterId == request.CurrentUserId);
             }

            result.Data=list.ToList();

           

            return Task.FromResult(result);
        }
    }
}
